home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / amiga / c_src2_5.zoo / evalexp.c < prev    next >
C/C++ Source or Header  |  1988-08-15  |  7KB  |  218 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25. #include "sim.h"
  26. #include "aux.h"
  27.  
  28. extern double floatval();
  29. extern word makefloat();
  30.  
  31. eval(op, val)
  32. word op, *val;
  33. {
  34.     register pw top;
  35.     struct psc_rec *p;
  36.     char *functor;
  37.     int arity, length, floatp;
  38.     word op1, op2;
  39.     double result;
  40.  
  41.     switch ((int)(op & 0x3)) {
  42.     case FREE:
  43.         printf("Error: unbound variable in arithmetic expression\n");
  44.         Fail0; return -1;
  45.     case CS:
  46.         floatp = 0;
  47.         p = get_str_psc(op);
  48.         functor = get_name(p);
  49.         arity = get_arity(p);
  50.         length = get_length(p);
  51.         if (length == 1)
  52.           switch (*functor) {
  53.         case '+': switch (arity) {
  54.             case 1:
  55.                 op1 = *(((pw)(untag(op))) + 1); deref(op1);
  56.                 return eval(op1, val);
  57.             case 2: 
  58.                 op1 = *(((pw)(untag(op))) + 1); deref(op1);
  59.                 floatp = eval(op1, &op1);
  60.                 op2 = *(((pw)(untag(op))) + 2); deref(op2);
  61.                 floatp = floatp | eval(op2, &op2);
  62.                 if (floatp >= 0) *val = makenum(numval(op1) + numval(op2));
  63.                 return floatp;
  64.             default: /* switch (arity) */
  65.                 printf("Error: unknown functor %s/%d in arithmetic expression\n", functor, arity);
  66.                 Fail0; return -1;
  67.             };
  68.         case '-':switch (arity) {
  69.             case 1:
  70.                 op1 = *(((pw)(untag(op))) + 1); deref(op1);
  71.                 floatp = eval(op1, &op1);
  72.                 if (floatp >= 0) {
  73.                 *val = makenum(-(numval(op1)));
  74.                 return floatp;
  75.                 }
  76.             case 2:
  77.                 op1 = *(((pw)(untag(op))) + 1); deref(op1);
  78.                 floatp = eval(op1, &op1);
  79.                 op2 = *(((pw)(untag(op))) + 2); deref(op2);
  80.                 floatp = floatp | eval(op2, &op2);
  81.                 if (floatp >= 0) *val = makenum(numval(op1) - numval(op2));
  82.                 return floatp;
  83.             default: /* switch (arity) */
  84.                 printf("Error: unknown functor %s/%d in arithmetic expression\n", functor, arity);
  85.                 Fail0; return -1;
  86.             };
  87.         case '*': if (arity == 2) {
  88.                 op1 = *(((pw)(untag(op))) + 1); deref(op1);
  89.                 floatp = eval(op1, &op1);
  90.                 op2 = *(((pw)(untag(op))) + 2); deref(op2);
  91.                 floatp = floatp | eval(op2, &op2);
  92.                 if (floatp >= 0) *val = makenum(numval(op1) * numval(op2));
  93.                 return floatp;
  94.             }
  95.         else {
  96.             printf("Error: unknown functor %s/%d in arithmetic expression\n", functor, arity);
  97.             Fail0; return -1;
  98.         };
  99.         case '/': if (arity == 2) {
  100.                 op1 = *(((pw)(untag(op))) + 1); deref(op1);
  101.                 floatp = eval(op1, &op1);
  102.                 op2 = *(((pw)(untag(op))) + 2); deref(op2);
  103.                 floatp = floatp | eval(op2, &op2);
  104.                 if (numval(op2) == 0) {
  105.                 printf("div: division by zero! ... failing execution ...\n");
  106.                 return -1;
  107.                 }
  108.                 if (floatp >= 0) {
  109.                 result = ((double)(numval(op1)))/((double)(numval(op2))) ;
  110.                 if (integral(result)) {
  111.                     floatp = 0;
  112.                     *val = makeint((int)result);
  113.                 }
  114.                 else {
  115.                     floatp = 1;
  116.                     *val = makefloat(result);
  117.                 }
  118.                 return floatp;
  119.                 }
  120.             }
  121.         else {
  122.             printf("Error: unknown functor %s/%d in arithmetic expression\n", functor, arity);
  123.             Fail0; return -1;
  124.         };
  125.         case '\\': if (arity == 1) {
  126.                 op1 = *(((pw)(untag(op))) + 1); deref(op1);
  127.                 floatp = eval(op1, &op1);
  128.                 if (floatp == 0) {
  129.                     *val = makeint(~(intval(op1)));
  130.                 return floatp;
  131.                 }
  132.                 else {printf("neg: integer required\n"); return -1;}
  133.             }
  134.         else {
  135.             printf("Error: unknown functor %s/%d in arithmetic expression\n", functor, arity);
  136.             Fail0; return -1;
  137.         };
  138.           }
  139.           else if (arity == 2) {
  140.         if (!strcmp(functor, "//") && arity == 2) {
  141.             op1 = *(((pw)(untag(op))) + 1); deref(op1);
  142.             floatp = eval(op1, &op1);
  143.             op2 = *(((pw)(untag(op))) + 2); deref(op2);
  144.             floatp = floatp | eval(op2, &op2);
  145.             if (floatp >= 0) *val = makeint(((int)(numval(op1) / numval(op2))));
  146.             return 0;
  147.             }
  148.         else if (!strcmp(functor, "mod") && arity == 2) {
  149.             op1 = *(((pw)(untag(op))) + 1); deref(op1);
  150.             floatp = eval(op1, &op1);
  151.             op2 = *(((pw)(untag(op))) + 2); deref(op2);
  152.             floatp = floatp | eval(op2, &op2);
  153.             if (floatp == 0) {
  154.                 *val = makeint(intval(op1) % intval(op2));
  155.                 return 0;
  156.             }
  157.             else {printf("mod: integer required\n"); return -1;}
  158.             }
  159.         else if (!strcmp(functor, "/\\") && arity == 2) {
  160.             op1 = *(((pw)(untag(op))) + 1); deref(op1);
  161.             floatp = eval(op1, &op1);
  162.             op2 = *(((pw)(untag(op))) + 2); deref(op2);
  163.             floatp = floatp | eval(op2, &op2);
  164.             if (floatp == 0) {
  165.                 *val = makeint(intval(op1) & intval(op2));
  166.                 return 0;
  167.             }
  168.             else {printf("and: integer required\n"); Fail0; return -1;}
  169.             }
  170.         else if (!strcmp(functor, "\\/") && arity == 2) {
  171.             op1 = *(((pw)(untag(op))) + 1); deref(op1);
  172.             floatp = eval(op1, &op1);
  173.             op2 = *(((pw)(untag(op))) + 2); deref(op2);
  174.             floatp = floatp | eval(op2, &op2);
  175.             if (floatp == 0) {
  176.                 *val = makeint(intval(op1) | intval(op2));
  177.                 return 0;
  178.             }
  179.             else {printf("or: integer required\n"); Fail0; return -1;}
  180.             }
  181.         else if (!strcmp(functor, ">>") && arity == 2) {
  182.             op1 = *(((pw)(untag(op))) + 1); deref(op1);
  183.             floatp = eval(op1, &op1);
  184.             op2 = *(((pw)(untag(op))) + 2); deref(op2);
  185.             floatp = floatp | eval(op2, &op2);
  186.             if (floatp == 0) {
  187.                 *val = makeint(intval(op1) >> intval(op2));
  188.                 return 0;
  189.             }
  190.             else {printf("rshift: integer required\n"); Fail0; return -1;}
  191.             }
  192.         else if (!strcmp(functor, "<<") && arity == 2) {
  193.             op1 = *(((pw)(untag(op))) + 1); deref(op1);
  194.             floatp = eval(op1, &op1);
  195.             op2 = *(((pw)(untag(op))) + 2); deref(op2);
  196.             floatp = floatp | eval(op2, &op2);
  197.             if (floatp == 0) {
  198.                 *val = makeint(intval(op1) << intval(op2));
  199.                 return 0;
  200.             }
  201.             else {printf("lshift: integer required\n"); Fail0; return -1;}
  202.             }
  203.         else {printf("Error: unknown functor %s/%d in arithmetic expression\n", functor, arity);
  204.             Fail0; return -1;
  205.             }
  206.         }
  207.          else {printf("Error: unknown functor %s/%d in arithmetic expression\n", functor, arity);
  208.             Fail0; return -1;
  209.         };
  210.     case NUM:
  211.         *val = op;
  212.         return isfloat(op);
  213.     case LIST:
  214.         Fail0; return -1;
  215.     };
  216. }
  217.  
  218.